package model;

import init.DBManagement;
import model.beans.PlanEstudioBean;

public class StudyPlan {
	public static PlanEstudioBean instance(String str_subjectCode){
		PlanEstudioBean studyPlan_retVal = null;
		String str_instance = 
			" select PLA_TECH_KEY, " +
			"        PROPOSITO, "+
			"        OBJ_TERMINAL, "+
			"        OBJ_INTELECTUAL, "+
			"        OBJ_SOCHUM, "+
			"        OBJ_PROFESIONAL, "+
			"        CONCEPTOS_PRE, "+
			"        CONCEPTOS_POST, "+
			"        ENCUADRE, "+
			"        TIEMPO_ENCUADRE, "+
			"        CRITERIOS_CALIFICA "+
			" from	 PLAMSTB "+
			" where  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    if( database.getNumRows() == 1 ){
	    	studyPlan_retVal = new PlanEstudioBean(
	    			    database.getItem(0, 0)!=null?Integer.parseInt(database.getItem(0, 0)):0,
		    			database.getItem(0, 1),
		    			database.getItem(0, 2),
		    			database.getItem(0, 3),
		    			database.getItem(0, 4),
		    			database.getItem(0, 5),
		    			database.getItem(0, 6),
		    			database.getItem(0, 7),
		    			database.getItem(0, 8),
		    			database.getItem(0, 9)!=null?Integer.parseInt(database.getItem(0, 9)):0,
		    			database.getItem(0, 10)
	    			);
	    }else{
	    	insertBlank(str_subjectCode);
	    	studyPlan_retVal = new PlanEstudioBean();
	    }
	    database = null;
		return studyPlan_retVal;
	}
	
	private static void insertBlank(String str_subjectCode){
		String str_instance = 
			" INSERT INTO PLAMSTB ( "+
			"        MATERIA_TK "+
			" ) VALUES ( "+
			"        '"+str_subjectCode+"' "+
			"  ) ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
	
	public static void saveGeneralGoal(String str_subjectCode, String str_generalGoal){
		String str_instance = 
			" UPDATE PLAMSTB "+
			" SET    PROPOSITO = '"+str_generalGoal+"' "+
			" WHERE  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
	
	public static void saveObjEnd(String str_subjectCode, String str_terminal, String str_intelectual, String str_socioHumano, String str_profesional){
		String str_instance = 
			" UPDATE PLAMSTB "+
			" SET    OBJ_TERMINAL = '"+str_terminal+"', "+
			"        OBJ_INTELECTUAL = '"+str_intelectual+"', "+
			"        OBJ_SOCHUM = '"+str_socioHumano+"', "+
			"        OBJ_PROFESIONAL = '"+str_profesional+"' "+
			" WHERE  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
	
	public static void saveConceptos(String str_subjectCode, String str_previos, String str_posteriores){
		String str_instance = 
			" UPDATE PLAMSTB "+
			" SET    CONCEPTOS_PRE = '"+str_previos+"', "+
			"        CONCEPTOS_POST = '"+str_posteriores+"' "+
			" WHERE  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
	
	public static void saveEncuadre(String str_subjectCode, String str_encuadre, int int_tiempoEncuadre){
		String str_instance = 
			" UPDATE PLAMSTB "+
			" SET    ENCUADRE = '"+str_encuadre+"', "+
			"        TIEMPO_ENCUADRE = "+int_tiempoEncuadre+" "+
			" WHERE  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
	
	public static void saveCriteriosCalificacion(String str_subjectCode, String str_criterios){
		String str_instance = 
			" UPDATE PLAMSTB "+
			" SET    CRITERIOS_CALIFICA = '"+str_criterios+"' "+
			" WHERE  MATERIA_TK = '"+str_subjectCode+"' ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    database = null;
	}
}